{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基本例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "data = np.random.random((1000,100))\n",
    "labels = np.random.randint(2,size=(1000,1))\n",
    "model = Sequential()\n",
    "model.add(Dense(32,activation='relu',input_dim=100))\n",
    "model.add(Dense(1, activation='sigmoid'))\n",
    "model.compile(optimizer='rmsprop',loss='binary_crossentroy', metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "keras数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.datasets import boston_housing, mnist, cifar10,imdb\n",
    "(x_train,y_train),(x_test,y_test) = mnist.load_data()\n",
    "(x_train2,y_train2),(x_test2,y_test2) = boston_housing.load_data()\n",
    "(x_train3,y_train3),(x_test3,y_test3) = cifar10.load_data()\n",
    "(x_train4,y_train4),(x_test4,y_test4) = imdb.load_data(num_words=20000)\n",
    "num_classes = 10\n",
    "model.fit(data,labels,epochs=10,batch_size=32)\n",
    "predictions = model.predict(data)\n",
    "\n",
    "from urllib.request import urlopen\n",
    "data = np.loadtxt(urlopen(\n",
    "    \"http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data\"),\n",
    "     delimiter=\",\")\n",
    "X = data[:,0:8]\n",
    "y = data [:,8]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型-顺序模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "model = Sequential()\n",
    "model2 = Sequential()\n",
    "model3 = Sequential()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型-多层感知机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Binary Classification\n",
    "from keras.layers import Dense\n",
    "model.add(Dense(12,input_dim=8,kernel_initializer='uniform',activation='relu'))\n",
    "model.add(Dense(8,kernel_initializer='uniform',activation='relu'))\n",
    "model.add(Dense(1,kernel_initializer='uniform',activation='sigmoid'))\n",
    "\n",
    "#Multi-Class Classification\n",
    "from keras.layers import Dropout\n",
    "model.add(Dense(512,activation='relu',input_shape=(784,)))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Dense(512,activation='relu'))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Dense(10,activation='softmax'))\n",
    "\n",
    "#Regression\n",
    "model.add(Dense(64,activation='relu',input_dim=train_data.shape[1]))\n",
    "model.add(Dense(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型-卷积神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.layers import Activation,Conv2D,MaxPooling2D,Flatten\n",
    "model2.add(Conv2D(32,(3,3),padding='same',input_shape=x_train.shape[1:]))\n",
    "model2.add(Activation('relu'))\n",
    "model2.add(Conv2D(32,(3,3)))\n",
    "model2.add(Activation('relu'))\n",
    "model2.add(MaxPooling2D(pool_size=(2,2)))\n",
    "model2.add(Dropout(0.25))\n",
    "model2.add(Conv2D(64,(3,3), padding='same'))\n",
    "model2.add(Activation('relu'))\n",
    "model2.add(Conv2D(64,(3, 3)))\n",
    "model2.add(Activation('relu'))\n",
    "model2.add(MaxPooling2D(pool_size=(2,2)))\n",
    "model2.add(Dropout(0.25))\n",
    "model2.add(Flatten())\n",
    "model2.add(Dense(512))\n",
    "model2.add(Activation('relu'))\n",
    "model2.add(Dropout(0.5))\n",
    "model2.add(Dense(num_classes))\n",
    "model2.add(Activation('softmax'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型-循环神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.klayers import Embedding,LSTM\n",
    "model3.add(Embedding(20000,128))\n",
    "model3.add(LSTM(128,dropout=0.2,recurrent_dropout=0.2))\n",
    "model3.add(Dense(1,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.optimizers import RMSprop\n",
    "opt = RMSprop(lr=0.0001, decay=1e-6)\n",
    "model2.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "early stopping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.callbacks import EarlyStopping\n",
    "early_stopping_monitor = EarlyStopping(patience=\n",
    "model3.fit(x_train4, y_train4, batch_size=32, epochs=15, \n",
    "           validation_data=(x_test4,y_test4),callbacks=[early_stopping_monitor])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编译模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#MLP: Binary Classification\n",
    "model.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])\n",
    "\n",
    "#MLP: Multi-Class Classification\n",
    "model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])\n",
    "\n",
    "#MLP: Regression\n",
    "model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])\n",
    "\n",
    "#Recurrent Neural Network\n",
    "model3.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "保存和加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.models import load_model\n",
    "model3.save('model_file.h5')\n",
    "my_model = load_model('my_model.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看模型详情"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.output_shape #Model output shape\n",
    "model.summary()     #Model summary representation\n",
    "model.get_config()   #Model configuration\n",
    "model.get_weights()  #List all weight tensors in the model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model3.predict(x_test4, batch_size=32)\n",
    "model3.predict_classes(x_test4,batch_size=32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model3.fit(x_train4,y_train4,batch_size=32,epochs=15,verbose=1,\n",
    "           validation_data=(x_test4,y_test4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "评价模型性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "score = model3.evaluate(x_test,y_test,batch_size=32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Sequence Padding\n",
    "from keras.preprocessing import sequence\n",
    "x_train4 = sequence.pad_sequences(x_train4,maxlen=80)\n",
    "x_test4 = sequence.pad_sequences(x_test4,maxlen=80)\n",
    "\n",
    "#One-Hot Encoding\n",
    "from keras.utils import to_categorical\n",
    "Y_train = to_categorical(y_train, num_classes)\n",
    "Y_test = to_categorical(y_test, num_classes)\n",
    "Y_train3 = to_categorical(y_train3, num_classes)\n",
    "Y_test3 = to_categorical(y_test3, num_classes)\n",
    "\n",
    "#Train and Test Sets\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train5,X_test5,y_train5,y_test5 = train_test_split(X,y,test_size=0.33,\n",
    "                                                     random_state=42)\n",
    "\n",
    "#Standardization/Normalization\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "scaler = StandardScaler().fit(x_train2)\n",
    "standardized_X = scaler.transform(x_train2)\n",
    "standardized_X_test = scaler.transform(x_test2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
